package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;

import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DeadlockLoserDataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);//查出分页数据

        List<DishVO> result = page.getResult();
        long total = page.getTotal();
        return new PageResult(total, result);
    }

    @Override
    public void addDish(DishDTO dishDTO) {
        String flavorName = dishDTO.getName();
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        Long categoryId = dish.getCategoryId();
//        System.out.println(dish);
//        System.out.println("categoryId"+categoryId);
        dish.setStatus(StatusConstant.ENABLE);
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        dish.setCreateUser(BaseContext.getCurrentId());
        dish.setUpdateUser(BaseContext.getCurrentId());
        System.out.println(dish);
        dishMapper.addDish(dish);//dish表
        Long dishId = getDishIdByName(dish.getName());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
                dishFlavorMapper.addDishFlavor(flavor);
            }
        }


    }

    @Override
    public Long getDishIdByName(String name) {
        return dishMapper.getDishIdByName(name);
    }

    @Override
    public Dish getDishById(Long id) {
        return dishMapper.getDishById(id);
    }

    @Override
    public void reviseDishInfo(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        dish.setCreateUser(BaseContext.getCurrentId());
        dish.setUpdateUser(BaseContext.getCurrentId());
        dishMapper.updateDish(dish);//更新dish
//        List<DishFlavor> flavors = dishDTO.getFlavors();
//        DishFlavor flavor = new DishFlavor();
//        for (DishFlavor dishFlavor : flavors) {
//            dishFlavorMapper.updateDishFlavor();
//        }
    }

    @Override
    public void stopAndStart(Integer status, Integer id) {
        dishMapper.stopAndStart(status, id);
    }

    @Override
    public void deleteDish(List<Long> ids) {
//        for (Integer id : ids) {
//            Dish dish = getDishById(id);
//            //判断菜品是否在售
//            if (dish.getStatus()== StatusConstant.ENABLE) {
//                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
//            }
//            dishMapper.deleteDish(id);
//        }
        for (Long id : ids) {
            Dish dish = getDishById(id);
        }
        dishMapper.deleteByIds(ids);
    }

    @Override
    public List<Dish> getDishList(Long id) {
        return dishMapper.getDishList(id);
    }

    @Override
    public DishOverViewVO getDishOver() {
        Integer sold = dishMapper.getDishOverSold();
        Integer disContinued = dishMapper.getDishOverdisContinued();
        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(disContinued)
                .build();
    }
}
